package com.xiaodou.mjf.application.user.service.impl;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.user.repository.MemberRepository;
import com.xiaodou.mjf.application.user.service.MemberService;
import com.xiaodou.mjf.entity.user.Member;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;


/***
 *
 * Member业务访问实现
 * @author XiaoDou.com Code Generator
 * @version 1.0
 * 2014-10-29 17:37:52
 *
 */
@Service @Transactional
public class MemberServiceImpl extends JpaSimpleService<Member,String> implements MemberService{

	Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);

    @Resource
    MemberRepository memberRepository;

    @Override
    protected JpaRepository<Member, String> getRepository() {
        return memberRepository;
    }  
  
	@Override 
    public Page<Member> page(Member entity) {
        return memberRepository.findAll((root,query,cb) -> { 
           Predicate p1 = cb.equal(root.get("status").as(Integer.class), entity.getStatus());
           Predicate p2 = cb.like(root.get("realName").as(String.class), "%" + entity.getRealName() + "%");
           if(entity.getStatus().equals(-1)){
         	  return query.where(p2).getRestriction();     
             }
            return query.where(p2,p1).getRestriction();
        },preparePage(entity,new Sort(new Sort.Order(Sort.Direction.ASC,"code"))));    }
	
}

